﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 版权所有 (c) 2022   保留所有权利。
 * CLR版本：4.0.30319.42000
 * 机器名称：DESKTOP-UL84BU5
 * 公司名称：
 * 命名空间：KingerRobot.WanliMDC.Biz
 * 唯一标识：24249019-4b0e-43cf-9e67-c03a7e5c182c
 * 文件名：LineBiz
 * 当前用户域：DESKTOP-UL84BU5
 * 
 * 创建者：zzl
 * 电子邮箱：1003590782@qq.com
 * 创建时间：2022/9/7 13:14:05
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
namespace KingerRobot.MexicoSailunMDC.Biz;

/// <summary>
/// 出库监控
/// </summary>
internal class LineBiz : K8.MDCBB.Biz
{
    private const string ClassName = nameof(TemplateBiz);
    private static ConcurrentDictionary<string, int?> directory = new();
    #region 配置参数
    private ParamData Param { get; set; }

    class ParamData
    {
        /// <summary>
        /// 线体点位编号
        /// </summary>
        public string LineCode { get; set; }
        /// <summary>
        /// 线体点位编号
        /// </summary>
        public long LineId { get; set; }

        public int? LineSign { get; set; } = 0;
    }
    #endregion

    #region PLC 通讯参数
    private ValueData Data { get; set; }
    class ValueData
    {
        /// <summary>
        /// 流水号
        /// </summary>
        public int? TaskNo { get; set; }

        /// <summary>
        /// 报警编号
        /// </summary>
        public int ErrorNo { get; set; }

        public int? rStatus { get; set; }
        ///// <summary>
        ///// 是否报警，1,-报警，0-不报警
        ///// </summary>
        //public int IsAlarm { get; set; }

        //public int? TargetCode { get; set; }
        //public int? TaskGroup { get; set; }
    }
    #endregion

    #region 缓存
    private CacheData Cache { get; set; }
    class CacheData
    {

    }
    #endregion

    #region 实时日志
    private StepData Step { get; set; }
    class StepData
    {
        private string _msg = string.Empty;
        public string StepMsg
        {
            get
            {
                return this._msg;
            }
            set
            {
                if (string.IsNullOrWhiteSpace(value))
                {
                    this._msg = string.Empty;
                    return;
                }
                this._msg = $"{ClassName}|{System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}>>{value}";
            }
        }
    }
    private void StepMsg(string msg)
    {
        this.Step.StepMsg = msg;
    }
    #endregion

    #region 文件日志（运行需要 Nlog 配置）
    private void WriteLog(string message)
    {
        K8.LSF.Client.Log(new K8.LSF.Log { Data = message });
    }
    #endregion

    /// <summary>
    /// 和 PLC 通讯对象
    /// </summary>
    private BizData BizData { get; set; }
    public int Execute(BizData writer)
    {
        // Excute 方法外使用 BizData
        this.BizData = writer;
        // 设备故障
        string errorCode = string.Empty;
        StringBuilder stringBuilder = new StringBuilder();
        var errorNo = 0; // 正常
        if (Data.ErrorNo != 1 && Data.ErrorNo != 0)
        {
            errorNo = Data.ErrorNo == 0 ? 11 : Data.ErrorNo;
            errorCode = errorNo.ToString();
            stringBuilder.Append($"[{Param.LineCode}] | 报警编号:{Data.ErrorNo}|{SwitchData.GetLineErrByCode(errorNo)}");
        }
        Alarm.Instance.AddAlarm(Param.LineId, errorNo, errorCode, stringBuilder.ToString());
        // 返回参数
        var result = 0;
        var lineCode = Param.LineCode;
        var lineSign = Param.LineSign;
        var taskNo = Data?.TaskNo ?? 0;
        var status = Data?.rStatus ?? 0;
        //有胎 无流水号的情况 默认流水号为-1
        if (lineSign == 1)
        {
            this.StepMsg($"线体{lineCode},无胎");
            if (status == 1)
            {
                this.StepMsg($"线体{lineCode},有胎无任务");
                taskNo = 1;
            }
        }
        //var targetCode=Data.TargetCode;
        //var taskGroup=Data.TaskGroup;
        // 判断是否初始数据
        this.StepMsg($"线体{lineCode},有胎无任务11");
        var flag = directory.TryGetValue(lineCode, out int? lineTaskNo);
        ScadaBoxTaskParam par = new ScadaBoxTaskParam()
        {
            TaskNo = taskNo,
            LineCode = lineCode,
            //TargetCode = targetCode,
            //TaskGroup = taskGroup,

        };
        this.StepMsg($"线体{lineCode},有胎无任务22");
        if (flag)
        {
            this.StepMsg($"线体{lineCode},有胎无任务33");
            if (taskNo == lineTaskNo)
            {
                return result;
            }
            // 修改数据
            var num = 0;
            var taskService = K8.RSF.Client.Create<ILineManagerService>();
            num = taskService.FlushScadaBoxTasks(par);
            this.StepMsg($"线体{lineCode},有胎无任务44,--{num}");
            if (num == 1)
            {
                this.StepMsg($"线体{lineCode},有胎无任务55");
                // 修改成功后 修改 字典值
                directory.AddOrUpdate(lineCode, taskNo, (k, v) => taskNo);
            }
        }
        else
        {
            this.StepMsg($"线体{lineCode},有胎无任务66");
            // 添加数据
            var num = 0;
            var taskService = K8.RSF.Client.Create<ILineManagerService>();
            num = taskService.FlushScadaBoxTasks(par);
            this.StepMsg($"线体{lineCode},有胎无任务77--{num}");
            if (num == 1)
            {
                this.StepMsg($"线体{lineCode},有胎无任务88--{num}");
                // 添加缓存
                directory.TryAdd(lineCode, taskNo);
            }
        }
        this.StepMsg($"线体{lineCode},有胎无任务99");
        return result;
    }
}
